from termcolor import cprint
import time
import os

logPath = ''
log2Console = True


def writeLog2File(text):
    if not logPath:
        return
    saveDir = logPath + '/' + dateFormat() + '/'
    if not os.path.exists(saveDir):
        os.makedirs(saveDir)
    logFilePath = saveDir + time.strftime('%H') + 'H.log'
    with open(logFilePath, 'a', encoding='utf-8') as logF:
        logF.write(text + '\n\n')
        logF.close()


def dateFormat() -> str:
    return time.strftime('%Y-%m-%d')


def timeFormat() -> str:
    return time.strftime('%Y-%m-%d %H:%M:%S')


def success(msg: str):
    text = '[{0} - {1}] {2}'.format(timeFormat(), 'SUCCESS', msg)
    if log2Console:
        cprint(text, 'green')
    writeLog2File(text)


def info(msg: str):
    text = '[{0} - {1}] {2}'.format(timeFormat(), 'INFO', msg)
    if log2Console:
        cprint(text, 'cyan')
    writeLog2File(text)


def warning(msg: str):
    text = '[{0} - {1}] {2}'.format(timeFormat(), 'WARNING', msg)
    if log2Console:
        cprint(text, 'yellow')
    writeLog2File(text)


def error(msg: str):
    text = '[{0} - {1}] {2}'.format(timeFormat(), 'ERROR', msg)
    if log2Console:
        cprint(text, 'red')
    writeLog2File(text)


def debug(msg: str):
    text = '[{0} - {1}] {2}'.format(timeFormat(), 'DEBUG', msg)
    if log2Console:
        print(text)
    writeLog2File(text)
